<?php

namespace App\Admin\Controllers\Users;

use App\Admin\Repositories\Users\TalentApply;
use App\Admin\Repositories\Users\WithdrawalLog;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WithdrawalLogController extends AdminController
{

    protected array $platform=[1=>'支付宝',2=>'微信'];
    protected array $status=[1=>'未审核',3=>'拒绝',4=>'审核通过'];


    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected function grid()
    {
        return Grid::make(new WithdrawalLog(), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('uid');
            $grid->column('account');
            $grid->column('amount');
            $grid->column('price');
            $grid->column('platform')->using($this->platform);
            $grid->column('status')->using($this->status);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->actions(function (Grid\Displayers\Actions $actions) { if ($actions->row->status != 1) { $actions->disableEdit(); } });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WithdrawalLog(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('account');
            $show->field('amount');
            $show->field('price');
            $show->field('platform');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WithdrawalLog(), function (Form $form) {
            $form->display('id');
            $form->display('uid');
            $form->display('account');
            $form->display('amount');
            $form->display('price');
            $form->select('platform')->options($this->platform);
            $form->select('status')->options($this->status)->saving(function ($v) use ($form) {
                if($v==2) {
                    $item = $form->model();
                    (new \App\Models\Users\WithdrawalLog())->refuse($item->id);
                }
                return $v;
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
